﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace WMM.Tapioca.Formula.Aspects
{
    /// <summary>
    /// An XPath OCC rule is an OCC rule that is expressed by the <formula:occXPath> element. 
    /// </summary>
    public class OccXpathAspect : OccAspect
    {
        private XElement element;

        internal XElement Element
        {
            get { return element; }
            set { element = value; }
        }

        private string select;

        /// <returns> An XPath expression in its @select attribute that, when evaluated,
        /// MUST yield a sequence of element nodes.  These element nodes (and their descendants,
        /// if any) are appended to an original OCC value to obtain a subsequent OCC value. </returns>
        public string Select
        {
            get { return select; }
            set { select = value; }
        }

        /// <summary>
        /// Constructor.
        /// </summary>
        public OccXpathAspect(XElement occXpathAspect)
        {
            this.Element = occXpathAspect;

            XElement aspect = occXpathAspect.Parent;

            if (aspect.Attribute("source") != null)
            {
                this.Source = aspect.Attribute("source").Value;
            }

            if (occXpathAspect.Attribute("source") != null)
            {
                this.Source = occXpathAspect.Attribute("source").Value;
            }

            XAttribute occ = occXpathAspect.Attribute("occ");

            if (occ != null)
            {
                this.Occ = occ.Value;
            }

            XAttribute select = occXpathAspect.Attribute("select");

            if (select != null)
            {
                this.Select = select.Value;

                if (occXpathAspect.Attribute("source") != null)
                {
                    this.Source = occXpathAspect.Attribute("source").Value;
                }
            }
        }
    }
}
